package com.lenovo.framework.util;

import android.util.LongSparseArray;
import android.view.View;

import com.baidu.platform.comjni.map.basemap.BaseMapCallback;
import com.baidu.platform.comjni.map.basemap.b;
import com.lenovo.framework.FApplication;
import com.lenovo.framework.biz.ContextManager;
import com.squareup.leakcanary.RefWatcher;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by hejie5 on 2016/3/21.
 */
public class BugFix {
    public static void destoryCallback(Object obj, View v){
        LongSparseArray<b> sparseArray =
                ReflectHelper.getFieldValue(new BaseMapCallback(), "a");
        DLog.i("sparsearray", "" + sparseArray);
        if(sparseArray != null){
            Method gc = ReflectHelper.getMethod(LongSparseArray.class, "gc");
            DLog.i("gc", "" + gc);
            try {
                DLog.i("fix callback", "clearing");
                gc.invoke(sparseArray);
            } catch (IllegalAccessException e) {
                DLog.p(e);
            } catch (InvocationTargetException e) {
                DLog.p(e);
            }
        }

        ((FApplication) ContextManager.appContext()).forceUnregisterComponentCallbacks();

        // Fixes android memory  issue 8488 :
        // http://code.google.com/p/android/issues/detail?id=8488
        if(v != null)
            ViewHelper.nullViewDrawablesRecursive(v);
        RefWatcher refWatcher = FApplication.getRefWatcher();
        refWatcher.watch(obj);

        System.gc();
        Runtime.getRuntime().gc();
    }
}
